package org.xm.sk.controller.dbase;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.xm.sk.exception.DataNotFoundException;
import org.xm.sk.service.data.BaseService;
import org.xm.sk.vo.Base;
import org.xm.sk.vo.ob.Ids;
import org.xm.sk.vo.ob.MapList;
import org.xm.sk.vo.ob.PageConf;
import org.xm.sk.vo.ob.ResultMsg;
import org.xm.sk.vo.tb.ServerTime;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@SuppressWarnings("unchecked")
@Api(tags = { "server_time" })
@RestController
@RequestMapping(value = "/serverTimes")
public class ServerTimeController {

    @Autowired
    private MessageSource messages;
    @Autowired
    private BaseService<ServerTime> serverTimeService;

    @ApiOperation(value = "创建服务时间", notes = "根据serverTime对象创建服务时间记录，使用此接口需要ADMIN权限")
    @ApiImplicitParam(name = "serverTime", value = "服务时间信息实体对象", required = true, dataType = "ServerTime")
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultMsg add(@RequestBody ServerTime serverTime)
            throws IOException, DataNotFoundException {
        if (null != serverTime.getId() && 0 < serverTime.getId().intValue()) {
            ServerTime u = (ServerTime) serverTimeService
                    .findById(serverTime.getId());
            if (Base.isValid(u)) {
                u.setName(serverTime.getName());
                serverTimeService.update(u);
            }
        } else {
            serverTimeService.add(serverTime);
        }
        return new ResultMsg("200",
                messages.getMessage("success", null, "SUCCESS", null),
                new Date());
    }

    @ApiIgnore()
    @ApiOperation(value = "删除服务时间", notes = "根据URI的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "服务时间ID", required = true, paramType = "path", dataType = "int")
    @PreAuthorize("hasRole('ADMIN')")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultMsg delete(@PathVariable Integer id) {
        serverTimeService.delete(id);
        return new ResultMsg("200",
                messages.getMessage("success", null, "SUCCESS", null),
                new Date());
    }

    @ApiOperation(value = "删除服务时间", notes = "根据POST的id列表来指定删除对象，可批量")
    @ApiImplicitParam(name = "ids", value = "服务时间ID集", required = true, dataType = "Ids")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResultMsg Batchdelete(@RequestBody Ids ids) {
        serverTimeService.delete(ids.getIds());
        return new ResultMsg("200",
                messages.getMessage("success", null, "SUCCESS", null),
                new Date());
    }

    @ApiOperation(value = "获取服务时间列表", notes = "根据提交的分页信息列出所以作品信息")
    @ApiImplicitParam(name = "pageConf", value = "分页信息实体", required = true, dataType = "PageConf")
    @RequestMapping(value = { "/list" }, method = RequestMethod.POST)
    public MapList<ServerTime> list(@RequestBody PageConf pageConf) {
        List<ServerTime> u = null;
        Long allCount = null;
        Object o = serverTimeService.findAllByPage(pageConf);
        if (o instanceof Page) {
            allCount = ((Page<ServerTime>) o).getTotalElements();
            u = ((Page<ServerTime>) o).getContent();
        } else {
            allCount = (long) ((List<ServerTime>) o).size();
            u = (List<ServerTime>) o;
        }
        return new MapList<ServerTime>(allCount, u);
    }

    // --------------------- 业务具体接口 ---------------------

}
